//
//  SLRootViewController.m
//  Objective-C
//
//  Created by S_Line on 2018/5/3.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "SLRootViewController.h"

#import "SLRootViewModel.h"

#import "SLRootCollectionHeaderView.h"
#import "SLRootCollectionCell.h"

@interface SLRootViewController ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic, strong) SLRootViewModel *viewModel;

@end

@implementation SLRootViewController

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)initNavBar {
    self.leftTitle = @" ";
    self.hideNavBar = true;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initNavBar];
    
    [self initData];
    
    [self initView];
}

- (void)initData {
    self.viewModel = [[SLRootViewModel alloc] init];
}

/**
 @property (nonatomic) CGFloat minimumLineSpacing;
 @property (nonatomic) CGFloat minimumInteritemSpacing;
 @property (nonatomic) CGSize itemSize;
 @property (nonatomic) CGSize estimatedItemSize NS_AVAILABLE_IOS(8_0); // defaults to CGSizeZero - setting a non-zero size enables cells that self-size via -preferredLayoutAttributesFittingAttributes:
 @property (nonatomic) UICollectionViewScrollDirection scrollDirection; // default is UICollectionViewScrollDirectionVertical
 @property (nonatomic) CGSize headerReferenceSize;
 @property (nonatomic) CGSize footerReferenceSize;
 @property (nonatomic) UIEdgeInsets sectionInset;
 */
- (void)initView {
    
    CGFloat cellMargin = 10;
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.headerReferenceSize = CGSizeMake(kScreenWidth, 44);
    layout.itemSize = CGSizeMake((kScreenWidth - cellMargin * 5) / 4, 44);
    layout.sectionInset = UIEdgeInsetsMake(0, cellMargin, cellMargin, cellMargin);
    self.collectionView.collectionViewLayout = layout;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"SLRootCollectionHeaderView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SLRootCollectionHeaderView"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"SLRootCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"SLRootCollectionCell"];
    if (@available(iOS 11.0, *)) {
        self.collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.bounces = true;
    
    [self.collectionView reloadData];
}

#pragma mark ----- UITableView的代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    NSInteger sections = [self.viewModel numberOfSections];
    NSLog(@"numberOfSectionsInCollectionView: %ld", (long)sections);
    return sections;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger rows = [self.viewModel numberOfRowsInSection:section];
    NSLog(@"numberOfItemsInSection: %ld, rows: %ld", section, rows);
    return rows;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cellForItemAtIndexPath");
    
    SLRootModelItem *modelItem = [self.viewModel objectForIndexPath:indexPath];
    
    SLRootCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SLRootCollectionCell" forIndexPath:indexPath];
    cell.modelItem = modelItem;

    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"----------> SLRootViewController - viewForSupplementaryElementOfKind: %@", kind);
    NSString *title = [self.viewModel titleForSection:indexPath.section];
    
    SLRootCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SLRootCollectionHeaderView" forIndexPath:indexPath];
    
    headerView.title = title;
    
    return headerView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    // 咪咕测试代码
//    NSMutableDictionary *action = [NSMutableDictionary new];
//    [action setValue:@"JUMP_DETAIL_PAGE" forKey:@"type"];
//    NSMutableDictionary *params = [NSMutableDictionary new];
//    [params setValue:@"default-frame" forKey:@"frameID"];
//    [params setValue:@"61fb7b0ec4c745bb97b90eb2b14b83c7" forKey:@"pageID"];
//    [params setValue:@"654456052" forKey:@"contentID"];
//    [action setValue:params forKey:@"params"];
//    NSString *urlString = [NSString stringWithFormat:@"miguvideo://miguvideo?action=%@", [action jsonStringEncoded]];
//    NSString *urlString1 = [urlString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
//    NSString *t = @"miguvideo://miguvideo?action=\"\"";
//
//    NSURL *url = [NSURL URLWithString:t];
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    
    NSLog(@"didSelectItemAtIndexPath.section: %ld, row: %ld", indexPath.section, indexPath.row);
    SLRootModelItem *modelItem = [self.viewModel objectForIndexPath:indexPath];
    
    [[SLVDNSManager shareInstance] openView:modelItem.routeK v:modelItem.routeV];
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    CGFloat cellMargin = 10;
//    return CGSizeMake((kScreenWidth - cellMargin * 5) / 4, 44);
//}
//
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
//    return CGSizeMake(kScreenWidth, 44);
//}


@end
